﻿package org.dm.3d.render 
{
	import flash.display.Scene;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.filters.BlurFilter;
	import flash.geom.Vector3D;
	import org.dm.3d.Actions.cameras.CAction;
	import org.dm.3d.object.shape.ObjectBase;
	import org.dm.3d.Actions.Action;
	import org.dm.3d.cameras.CameraBase;
	import flash.utils.getTimer;
	import org.dm.3d.object.shape.SceneContainer;
	
	/**
	 * ...
	 * @author Scott
	 */
	public class RenderShape extends Sprite
	{
		private var _items:Array;
		private var _num:uint;
		private var _itemsAction:Action;
		private var _camera:CameraBase;
		private var _cameraAction:CAction;
		public var _curTime:uint;
		private var _isrunning:Boolean = false;
		private var _scene:SceneContainer;
		
		public function RenderShape(pcamera:CameraBase,pitem:Array=null) 
		{
			_camera = pcamera;
			_items = (pitem != null)?pitem:new Array();
			_num = _items.length;
			_itemsAction = new Action();
			_cameraAction = new CAction();
			if(_num>0) draw();
		}
		protected function draw():void 
		{
			addEventListener(Event.ENTER_FRAME, update);
			for (var i:uint; i < _num; i++)
				this.addChild(_items[i]);			
		}
		public function addItem(pitem:ObjectBase=null):void 
		{
			if (pitem != null) 
			{
				this._items.push(pitem);
				_num++;
			}
		}
		
		public function removeItem(pindex:uint=0):void
		{
			if (pindex > _num || pindex < 1) return;
			_items.splice(pindex - 1, 1);
			_num--;
		}
		
		public function setItemAction(action:Action=null):void 
		{
			_itemsAction = (action != null)? action:new Action();
			_curTime = getTimer();
		}
		
		public function disEAction():void
		{
			if (_itemsAction != null) _itemsAction.isUsed = false;
		}
		
		public function setCAction(action:CAction=null):void 
		{
			_cameraAction = (action != null)? action:new CAction();
		}
		public function disCAction():void
		{
			if (_cameraAction != null) _cameraAction.isUsed = false;
		}
		private function run():void 
		{
			
		}
		public function update(e:Event):void
		{
			var i:uint;
			var j:uint;
			var time:Number = (getTimer() - _curTime)*.001;	
			if (_itemsAction.isUsed) _itemsAction.update(_items, time,_num);
			//if (_cameraAction.isUsed) _cameraAction.update(_camera, time);
			transForshow();
		}
		private function transForshow():void 
		{
			var cpos:Vector3D;
			var tempx:Number;
			var tempy:Number;
			var angle:Number;
			var focal:Number = _camera.fLength;
			for (var i:uint = 0; i <_num; i++)
			{
				cpos = _items[i].pos.subtract(_camera.pos);
				angle = _camera.angle.x;
				tempx = Math.cos(angle)*cpos.x - Math.sin(angle)*cpos.z;
				tempy = Math.sin(angle)*cpos.x + Math.cos(angle)*cpos.z;
				cpos.x = tempx;
				cpos.z = tempy;
    
				angle = _camera.angle.y;                   // the same thing we have for pitch angle
				tempx = Math.cos(angle)*cpos.y - Math.sin(angle)*cpos.z;
				tempy = Math.sin(angle)*cpos.y + Math.cos(angle)*cpos.z;
				cpos.y = tempx;
				cpos.z = tempy;
				
				angle = _camera.angle.z;                          // and tilt angle
				tempx = Math.cos(angle)*cpos.x - Math.sin(angle)*cpos.y;
				tempy = Math.sin(angle)*cpos.x + Math.cos(angle)*cpos.y;
				cpos.x = tempx;
				cpos.y = tempy;
				
				if (cpos.z < -focal) 
				{
					_items[i].visible = false;
					continue;
				}
				//if (cpos.z < -focal + 200) _items[i].filters = [new BlurFilter(cpos.z / 100, cpos.z / 100, 2)];
				angle = focal / (focal + cpos.z);
				_items[i].x = cpos.x * angle;
				_items[i].y = cpos.y * angle;
				_items[i].scaleX = _items[i].scaleY = angle;
				
			}
		}
	}
	
}